<%@page import="java.util.*"%>
<%@page import="java.text.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%
    String path = request.getContextPath(); 
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+path+ "/"; 
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<title>添加章节</title>
	<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="bootstrap-table/dist/bootstrap-table.css">
    <link rel="stylesheet" type="text/css" href="css/xcConfirm.css" />
	<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400,300" />
	<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
	<link rel="stylesheet" href="bootstrap-fileinput-master/css/fileinput.min.css">
	<link href="umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
    
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="css/font.css">
    <link rel="stylesheet" href="css/x-admin.css">

<link href="http://jssdk.demo.qiniu.io/images/favicon.ico" rel="shortcut icon">

<!-- 加载需要的js -->
<script type="text/javascript" src="js/jquery.min.js"></script>
		<script src="js/common2.js"></script>
		<script src="lib/layui/layui.js" charset="utf-8"></script>
        <script src="js/x-layui.js" charset="utf-8"></script>
		<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"/></script>
		<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
		<script src="js/jquery-bootstrap-pagination.js"></script>
		<script src="js/bootstrap-datetimepicker.js"></script>
		<script src="js/bootstrap-datetimepicker.zh-CN.js"></script>
		<script src="js/plupload.full.min.js"></script>
    	<script type="text/javascript" src="http://jssdk.demo.qiniu.io/dist/qiniu.min.js"></script>
    
    <style type="text/css">
        #panel {
            position: absolute;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }
        .td1{
			float:left;
			width:50px;
			display: block; 
			valign:middle;
		}
		.td2{
			float:left;
			width:200px;
			display: block; 
			valign:middle;
		}
		.td3{
			float:left;
			width:660px;
			display: block; 
			valign:middle;
		}
    </style>

</head> 

<body>
		<div class="x-body">
            <div   class="form-horizontal" role="form">
            <div class="form-group" style="">
            	<label for="firstname" class="col-sm-2 control-label">视频名称:</label>
			    <div class="col-sm-3">
			      <input type="text" class="form-control" id="title" name="title" required="required" placeholder="请输入视频名称"/>
			      <input type="hidden" id="tokens">
			    </div>
            	<label for="firstname" class="col-sm-2 control-label">可得积分:</label>
			    <div class="col-sm-3">
			      <input type="text" class="form-control" id="get_integral" name="get_integral" required="required" placeholder="请输入正整数"/>
			    </div>
            	<label for="firstname" class="col-sm-2 control-label">可得成长值:</label>
			    <div class="col-sm-3">
			      <input type="text" class="form-control" id="get_growth_value" name="get_growth_value" required="required" placeholder="请输入正整数"/>
			    </div>
            	<label for="firstname" class="col-sm-2 control-label">视频金额(如果金额为0则积分也为0):</label>
			    <div class="col-sm-3">
			      <input type="text" class="form-control" id="G_LESSON_MONEY" name="G_LESSON_MONEY" required="required" placeholder="请输入视频金额"/>
			    </div>
            	<label for="firstname" class="col-sm-2 control-label">是否积分购买:</label>
			    <div class="col-sm-3">
			      <input type="text" class="form-control" id="G_LESSON_INTEGEAL" name="G_LESSON_INTEGEAL" required="required" placeholder="如果是请输入积分，否则请输0"/>
			    </div>
			    <label for="firstname" class="col-sm-2 control-label">视频讲师:</label>
			    <div class="col-sm-3">
			      <select id="teacher_name" class="form-control">
              	  </select>
			    </div>
			    <label for="firstname" class="col-sm-2 control-label">视频简介:</label>
			    <div class="col-sm-3">
			      <input type="text" class="form-control" id="content" name="content" required="required" placeholder="请输入视频简介"/>
			    </div>
            </div>
            <br><br>
    			<div class="form-group">
	    			<div class="col-sm-3" style="top:440px;height:200px;" id="container">
		        		<button class="btn btn-info"  id="pickfiles">请选择视频文件</button>
		    			<button class="btn btn-info"  id="uploadfiles">上传</button>
		    			<p id="text1"></p>
		        		<!-- <ul id="file-list">
		        		</ul> -->
		        		<br>
		        		<input type="text" class="form-control" id="key" name="key" disabled="disabled" placeholder="文件名称"/>
		        		<br>
		        		<input type="hidden" class="form-control" id="size" name="size" disabled="disabled" placeholder="文件大小"/>
		        		<input type="text" class="form-control" id="url" name="url" disabled="disabled" placeholder="文件路径"/>
		    		</div>
    			</div>
    			
    			<!-- <div class="form-group">
			    <label for="firstname" style="margin-top: 120px;" class="col-sm-2 control-label">是否试看:</label>
			    <div class="col-sm-3" style="margin-top: 10px;">
			      <input type="radio" name="isWatch" id="isWatch"  value="0" checked="checked" onclick="divClick()" />不能试看&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			      <input type="radio" name="isWatch" id="isWatch"  value="1" onclick="divClick()" />可以试看
			    </div>
			  </div>
    			
    			<div class="form-group" id="com">
	    			<div class="col-sm-3" style="top:55px" id="container1">
		    			<button class="btn btn-info"  id="pickfiles2">请选择试看视频</button>
		    			<button class="btn btn-info"  id="uploadfiles2">上传</button>
		    			<p id="text2"></p>
		    			<br>
		        		<input type="text" class="form-control" id="freeKey" name="freeKey" disabled="disabled" placeholder="文件名称"/>
		        		<br>
		        		<input type="hidden" class="form-control" id="freeSize" name="freeSize" disabled="disabled" placeholder="文件大小"/>
		        		<input type="text" class="form-control" id="freeUrl" name="freeUrl" disabled="disabled" placeholder="文件路径"/>
		    		</div>
    			</div> -->
    			
    			<div class="form-group"  style="padding-left: 30%;top:300px;margin-top: 150px;z-index:100;">
        
        			<button type="button"   onclick="save()"  class="btn btn-info" >保存</button>
        			<button type="reset"    onclick="cancel()"  class="btn btn-info" >取消 </button>
   
     			</div>
    		</div>
    	</div>

    <script>
    $(function(){
   		$.ajax({
	        type: "get",
	        url: "courseInfo/selectTeacherList",
	        dataType:"json",
	        success: function(data){    
	      	  if(data.code==200){	           	              
	      		var htm='';
	      		    htm +='<option value="">请选择视频讲师</option>';
	     		for (var i = 0; i < data.data.length; i++) {
	   				var bean = data.data;
	   				htm +='<option value="'+bean[i].G_ID+'">'+bean[i].G_NAME+'</option>';
				}
	     		$("#teacher_name").html(htm);
	   	 	  }
	 	      
	        }
	    });
   	   });
    
    $("#title").change(function(){
    	var G_LESSON_NAME = $("#title").val();
    	$.ajax({
	        type: "get",
	        url: "video/selectVideoName",
	        data:{
	        	G_LESSON_NAME:G_LESSON_NAME,
	        },
	        dataType:"json",
	        success: function(data){    
	      	  if(data.code == 100){	           	              
	      		var txt = data.msg;
	      	    layer.alert(txt); 
	      	  	$("#title").val("")
	   	 	  }
	        }
	    });
    })
    
    $("#G_LESSON_MONEY").change(function(){
    	var v1 = $("#G_LESSON_MONEY").val();
    	if(v1 == 0){
    		$("#G_LESSON_INTEGEAL").val("0");
    	}
    })
    
    
    
    layui.use(['form','layer','upload'], function(){
        $ = layui.jquery;
      var form = layui.form()
      ,layer = layui.layer;
      });
    
    function save(){
		   	 var title = $("#title").val();
		   	 if(title == null||title=="") {
		   		layer.msg('视频名称不能为空!',{icon:2,time:2000});
			  return ;
		   	 }
		   	 
		   	 var get_growth_value = $("#get_growth_value").val();
		   	 if(get_growth_value == null||get_growth_value=="") {
		   		layer.msg('可得成长值不能为空!',{icon:2,time:2000});
			  return ;
		   	 }
		   	 var get_integral = $("#get_integral").val();
		   	 if(get_integral == null||get_integral=="") {
		   		layer.msg('可得积分不能为空!',{icon:2,time:2000});
			  return ;
		   	 }
		   	 var G_LESSON_MONEY = $("#G_LESSON_MONEY").val();
		   	 if(G_LESSON_MONEY == null||G_LESSON_MONEY=="") {
		   		layer.msg('视频金额不能为空!',{icon:2,time:2000});
			  return ;
		   	 }
		   	 var G_LESSON_INTEGEAL = $("#G_LESSON_INTEGEAL").val();
		   	 if(G_LESSON_INTEGEAL == null||G_LESSON_INTEGEAL=="") {
		   		layer.msg('视频积分不能为空!',{icon:2,time:2000});
			  return ;
		   	 }
		   	 var teacher_id = $("#teacher_name").val();
			  if(teacher_id == "") {
				layer.msg('请选择视频讲师!',{icon:2,time:2000});
			  return ;
		   	 } 
		   	 var content = $("#content").val();
			 var teacher_name = $("#teacher_name option:selected").text();
			 
			 var url =	$("#url").val();
			  if(url == null||url=="") {
				layer.msg('视频路径不能为空!',{icon:2,time:2000});
			  return ;
		   	 } 
			  var size =	$("#size").val();
			  if(size == null||size=="") {
				layer.msg('视频大小不能为空!',{icon:2,time:2000});
			  return ;
		   	 } 
			  var key =	$("#key").val();
			  if(key == null||key=="") {
				layer.msg('视频名称不能为空!',{icon:2,time:2000});
			  return ;
		   	 } 
			 
			  var isWatch =	$("input[name='isWatch']:checked").val();
			  var freeUrl = "";
			  var freeKey = "";
			  var freeSize = "";
			  if(isWatch == 1) {
				  freeKey =	$("#freeKey").val();
				  if(freeKey == null||freeKey=="") {
						layer.msg('试看视频名称不能为空!',{icon:2,time:2000});
					  return ;
				   } 
				  freeUrl =	$("#freeUrl").val();
				  if(freeUrl == null||freeUrl=="") {
						layer.msg('试看视频路径不能为空!',{icon:2,time:2000});
					  return ;
				   } 
				  freeSize =	$("#freeSize").val();
				  if(freeSize == null||freeSize=="") {
						layer.msg('试看视频大小不能为空!',{icon:2,time:2000});
					  return ;
				   } 
			  }
			  
          	//layer.confirm('请确认无误后添加',function(index){
    			$.ajax({
    	            url: "video/insertVideo",
    	            type: "post",
    	            data: {
	   	            	 title:title,
	   	            	 get_integral:get_integral,
	   	            	 get_growth_value:get_growth_value,
	   	            	 url:url,
	   	            	 G_LESSON_MONEY:G_LESSON_MONEY,
	   	            	 G_LESSON_INTEGEAL:G_LESSON_INTEGEAL,
	   	            	 key:key,
	   	            	 freeUrl:freeUrl,
	   	            	 size:size,
	   	            	 teacher_id:teacher_id,
	   	            	 teacher_name:teacher_name,
	   	            	 content:content,
	   		         },
    	            success: function(data) {
    	            	if(data.code==200){
    		           		layer.alert("添加成功", {icon: 6},function () {
    			           		parent.location.reload();
    		                   	// 获得frame索引
    		                  	var index = parent.layer.getFrameIndex(window.name);
    		                  	//关闭当前frame
    		                  	parent.layer.close(index);
    		                    parent.location.reload();
    	              		});    
    			      	}else{
    			   	      	var txt=data.msg;
    		     	    	layer.alert(txt); 
    		     	    }
    			 	} 
    	        });
           // });
		          	 
	        /*  $.ajax({
	             type: "post",
	             url: "/video/insertVideo",
	             data: {
	            	 title:title,
	            	 G_LESSON_CATALOG:G_LESSON_CATALOG,
	            	 url:url,
	            	 G_LESSON_MONEY:G_LESSON_MONEY,
	            	 G_LESSON_INTEGEAL:G_LESSON_INTEGEAL,
	            	 freeUrl:freeUrl
		         },
	             dataType:"json",
	             success: function(data){    
		           	 if(data.code==200){
		           		layer.alert("增加成功", {icon: 6},function () {
			           		parent.location.reload();
		                   	// 获得frame索引
		                  	var index = parent.layer.getFrameIndex(window.name);
		                  	//关闭当前frame
		                  	parent.layer.close(index);
		                    parent.location.reload();
	              		});    
			      	}else{
			   	      	var txt=data.msg;
		     	    	layer.alert(txt); 
		     	    }
		        }
			});   */
		}
	   
	   function cancel(){
		   	
		// 获得frame索引
      var index = parent.layer.getFrameIndex(window.name);
      //关闭当前frame
      parent.layer.close(index);
	         
			}
    
    function divClick(){    
	    
	       var show="";    
	       var apm = document.getElementsByName("isWatch");    
	       for(var i=0;i<apm.length;i++){    
	       if(apm[i].checked)    
	        show = apm[i].value;    
	       }    
	         
	       switch (show){    
	           case '0':    
	               document.getElementById("com").style.display="none";    
	               break;    
	           default:    
	               document.getElementById("com").style.display="block";    
	               break;                                                       
	       }    
	}
    
     $(function(){
    	 $.ajax({
             type: "get",
             url: "qiniu/uptoken",
             dataType:"json",
             success: function(data){    
           	  if(data.code==200){	           	              
          		$("#tokens").val(data.data);
        	 	  }
      	      
             }
         });
    }); 
    
    function getToken() {
    	$.ajax({
            type: "get",
            url: "qiniu/uptoken",
            dataType:"json",
            success: function(data){    
           	
          	  if(data.code==200){	           	              
         		$("#tokens").val(data.data);
       	 	  }
     	      
            }
        });
    }

     $(function(){
     	
     	var show="";    
	       var apm = document.getElementsByName("isWatch");    
	       for(var i=0;i<apm.length;i++){    
	       if(apm[i].checked)    
	        show = apm[i].value;    
	       }    
	         
	       switch (show){    
	           case '0':    
	               document.getElementById("com").style.display="none";    
	               break;    
	           default:    
	               document.getElementById("com").style.display="block";    
	               break;                                                       
	       }    
     	
     });
        //引入Plupload 、qiniu.js后
        var uploader = Qiniu.uploader({
            runtimes: 'html5,flash,html4', //上传模式,依次退化
            browse_button: 'pickfiles', //上传选择的点选按钮，**必需**
            //uptoken: 'pRyQPoTs_lqt02zxHGyriLUufn_RXvsDTkNQnkms:VMTl7m8fFl6ahK0d9ez7jHMdyHY=:eyJzY29wZSI6ImFpbGFpa2V2aWRlbyIsImRlYWRsaW5lIjoxNTE5ODE2OTk3fQ==',
            //uptoken: $("#tokens").val(),
             uptoken_url:'qiniu/uptoken', //Ajax请求upToken的Url，**强烈建议设置**（服务端提供）
            // uptoken : '', //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成
             unique_names: true, // 默认 false，key为文件名。若开启该选项，SDK为自动生成上传成功后的key（文件名）。
            // save_key: true, // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`，则开启，SDK会忽略对key的处理
            domain: 'http://bbs.shituzx.com/', //bucket 域名，下载资源时用到，**必需**
            get_new_uptoken: true, //设置上传文件的时候是否每次都重新获取新的token
            container: 'container', //上传区域DOM ID，默认是browser_button的父元素，
            max_file_size: '2000mb', //最大文件体积限制
            flash_swf_url: 'Moxie.swf', //引入flash,相对路径
            max_retries: 3, //上传失败最大重试次数
            dragdrop: true, //开启可拖曳上传
            drop_element: 'container', //拖曳上传区域元素的ID，拖曳文件或文件夹后可触发上传
            chunk_size: '4mb', //分块上传时，每片的体积
            auto_start: false, //选择文件后自动上传，若关闭需要自己绑定事件触发上传
            filters : {//限制文件上传的格式
            	max_file_size : '2000mb',
                prevent_duplicates: true,
                //Specify what files to browse for
                 mime_types: [
                {title : "Video files", extensions : "flv,mpg,mpeg,avi,wmv,mov,asf,rm,rmvb,mkv,m4v,mp4"}, //限定flv,mpg,mpeg,avi,wmv,mov,asf,rm,rmvb,mkv,m4v,mp4后缀格式上传
                ]
            },
            init: {
                'FilesAdded': function (up, files) {
                    plupload.each(files, function (file) {
                        // 文件添加进队列后,处理相关的事情
                        console.log(file.name);
                        $("#text1").html("等待.....")
                    });
                },
                'BeforeUpload': function (up, file) {
                    // 每个文件上传前,处理相关的事情
                },
                'UploadProgress': function (up, file) {
                    // 每个文件上传时,处理相关的事情
                    $("#text1").html("上传中.....")
                },
                'FileUploaded': function (up, file, info) {
                	$("#text1").html("上传成功")
                    console.log(up)
                    console.log(file)
                    console.log(info)
                    // 每个文件上传成功后,处理相关的事情
                    // 其中 info 是文件上传成功后，服务端返回的json，形式如
                    // {
                    // "hash": "Fh8xVqod2MQ1mocfI4S4KpRL6D98",
                    // "key": "gogopher.jpg"
                    // }
                    // 参考http://developer.qiniu.com/docs/v6/api/overview/up/response/simple-response.html

                    var domain = up.getOption('domain');
                    var res = JSON.parse(info.response);
                    var sourceLink = domain + res.key; //获取上传成功后的文件的Url
                    console.log(res.key);
                    $("#key").val(res.key);
                    $("#url").val(sourceLink);
                    $("#size").val(file.size);
                },
                'Error': function (up, err, errTip) {
                    //上传出错时,处理相关的事情
                    $("#text1").html("上传失败")
                },
                'UploadComplete': function () {
                    //队列文件处理完毕后,处理相关的事情
                },
                /* 'Key': function (up, file) {
                    // 若想在前端对每个文件的key进行个性化处理，可以配置该函数
                    // 该配置必须要在 unique_names: false , save_key: false 时才生效

                    var key = "12.png";
                    // do something with key here
                    return key
                }*/
            }
        });
       /*  var uploader2 = Qiniu.uploader({
            runtimes: 'html5,flash,html4', //上传模式,依次退化
            browse_button: 'pickfiles2', //上传选择的点选按钮，**必需**
            //uptoken: 'pRyQPoTs_lqt02zxHGyriLUufn_RXvsDTkNQnkms:VMTl7m8fFl6ahK0d9ez7jHMdyHY=:eyJzY29wZSI6ImFpbGFpa2V2aWRlbyIsImRlYWRsaW5lIjoxNTE5ODE2OTk3fQ==',
            //uptoken: $("#tokens").val(),
            //uptoken_func:getToken(),
             uptoken_url:'qiniu/uptoken', //Ajax请求upToken的Url，**强烈建议设置**（服务端提供）
            // uptoken : '', //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成
             unique_names: true, // 默认 false，key为文件名。若开启该选项，SDK为自动生成上传成功后的key（文件名）。
            // save_key: true, // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`，则开启，SDK会忽略对key的处理
            domain: 'http://www.shituzx.com/', //bucket 域名，下载资源时用到，**必需**
            get_new_uptoken: true, //设置上传文件的时候是否每次都重新获取新的token
            container: 'container1', //上传区域DOM ID，默认是browser_button的父元素，
            max_file_size: '100mb', //最大文件体积限制
            flash_swf_url: 'Moxie.swf', //引入flash,相对路径
            max_retries: 3, //上传失败最大重试次数
            dragdrop: true, //开启可拖曳上传
            drop_element: 'container1', //拖曳上传区域元素的ID，拖曳文件或文件夹后可触发上传
            chunk_size: '4mb', //分块上传时，每片的体积
            auto_start: false, //选择文件后自动上传，若关闭需要自己绑定事件触发上传
            filters : {//限制文件上传的格式
            	max_file_size : '2000mb',
                prevent_duplicates: true,
                //Specify what files to browse for
                 mime_types: [
                {title : "Video files", extensions : "flv,mpg,mpeg,avi,wmv,mov,asf,rm,rmvb,mkv,m4v,mp4"}, //限定flv,mpg,mpeg,avi,wmv,mov,asf,rm,rmvb,mkv,m4v,mp4后缀格式上传
                ]
            },
            init: {
                'FilesAdded': function (up, files) {
                    plupload.each(files, function (file) {
                        // 文件添加进队列后,处理相关的事情
                        console.log(file.name);
                        $("#text2").html("等待....")
                    });
                },
                'BeforeUpload': function (up, file) {
                    // 每个文件上传前,处理相关的事情
                },
                'UploadProgress': function (up, file) {
                    // 每个文件上传时,处理相关的事情
                    $("#text2").html("上传中....")
                },
                'FileUploaded': function (up, file, info) {
                	$("#text2").html("上传成功")
                    console.log(up)
                    console.log(file)
                    console.log(info)
                    // 每个文件上传成功后,处理相关的事情
                    // 其中 info 是文件上传成功后，服务端返回的json，形式如
                    // {
                    // "hash": "Fh8xVqod2MQ1mocfI4S4KpRL6D98",
                    // "key": "gogopher.jpg"
                    // }
                    // 参考http://developer.qiniu.com/docs/v6/api/overview/up/response/simple-response.html

                    var domain = up.getOption('domain');
                    var res = JSON.parse(info.response);
                    var sourceLink = domain + res.key; //获取上传成功后的文件的Url
                    console.log(sourceLink)
                    $("#freeKey").val(res.key);
                    $("#freeUrl").val(sourceLink);
                    $("#freeSize").val(file.size);
                },
                'Error': function (up, err, errTip) {
                    //上传出错时,处理相关的事情
                    $("#text2").html("上传失败")
                },
                'UploadComplete': function () {
                    //队列文件处理完毕后,处理相关的事情
                },
                 'Key': function (up, file) {
                    // 若想在前端对每个文件的key进行个性化处理，可以配置该函数
                    // 该配置必须要在 unique_names: false , save_key: false 时才生效

                    var key = "";
                    // do something with key here
                    return key
                } 
            }
        }); */


        // domain 为七牛空间（bucket)对应的域名，选择某个空间后，可通过"空间设置->基本设置->域名设置"查看获取

        // uploader 为一个plupload对象，继承了所有plupload的方法，参考http://plupload.com/docs


         //function getToken() {
           //  var token = {};
             //$.ajax({
               //  url: 
                 //async: false,
                //success: function (data) {
                  //   var obj = JSON.parse(data);
                    // token.token = obj.uploadToken;
                     //token.filename = obj.filename;

                //}
             //})
             //return token;
         //}
        //document.getElementById('uploadfiles').onclick = function () {
          //  uploader.start();
        //};
        //document.getElementById('uploadfiles2').onclick = function () {
          //  uploader2.start();
        //};
        $("#uploadfiles").click(function(){
        	uploader.start();
        });
        $("#uploadfiles2").click(function(){
        	uploader2.start();
        });
    </script>
  
  
  
</body>  

</html>